<template>
  <el-dialog v-model="LogExport" width="500">
    <el-form :model="ExportCouts" label-width="auto" style="max-width: 600px">
      <el-form-item label="导出数量:">
        <el-input v-model="ExportCouts.cout" type="text" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="AllExport()">导出</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

  <!-- 新增角色 -->
  <el-dialog v-model="AdddialogVisible" title="Tips" width="1000px">
    <el-form :model="AddForm" label-width="auto">
      <el-form-item label="产品编号">
        <el-input v-model="AddForm.product_Code" style="width: 200px;" placeholder="请输入产品编号" />
      </el-form-item>

      <el-form-item label="产品名称">
        <el-input v-model="AddForm.product_Name" style="width: 200px;" placeholder="请输入产品名称" />
      </el-form-item>
      <el-form-item label="产品规格">
        <el-input v-model="AddForm.product_Spec" style="width: 200px;" placeholder="请输入产品规格" />
      </el-form-item>
      <el-form-item label="类别">
        <el-input v-model="AddForm.category" style="width: 200px;" placeholder="请输入类别" />
      </el-form-item>
      <el-form-item label="存储单位">
        <el-input v-model="AddForm.storage_Unit" style="width: 200px;" placeholder="请输入存储单位" />
      </el-form-item>
      <el-form-item label="库存数量">
        <el-input v-model="AddForm.inventory" style="width: 200px;" placeholder="请输入库存数量" />
      </el-form-item>


      <el-form-item>
        <el-button @click="AdddialogVisible = false">取消</el-button>
        <el-button type="primary" @click="AddOppen()">新增</el-button>
      </el-form-item>

    </el-form>

  </el-dialog>
  <!-- 修改角色 -->
  <el-dialog v-model="UpdatedialogVisible" title="Tips" width="1000px">
    <el-form :model="UpdateForm" label-width="auto">
      <el-form-item label="产品编号">
        <el-input v-model="UpdateForm.product_Code" style="width: 200px;" placeholder="请输入产品编号" />
      </el-form-item>

      <el-form-item label="产品名称">
        <el-input v-model="UpdateForm.product_Name" style="width: 200px;" placeholder="请输入产品名称" />
      </el-form-item>
      <el-form-item label="产品规格">
        <el-input v-model="UpdateForm.product_Spec" style="width: 200px;" placeholder="请输入产品规格" />
      </el-form-item>
      <el-form-item label="类别">
        <el-input v-model="UpdateForm.category" style="width: 200px;" placeholder="请输入类别" />
      </el-form-item>
      <el-form-item label="存储单位">
        <el-input v-model="UpdateForm.storage_Unit" style="width: 200px;" placeholder="请输入存储单位" />
      </el-form-item>
      <el-form-item label="库存数量">
        <el-input v-model="UpdateForm.inventory" style="width: 200px;" placeholder="请输入库存数量" />
      </el-form-item>

      <el-form-item>
        <el-button @click="UpdatedialogVisible = false">取消</el-button>
        <el-button type="primary" @click="EditAll()">修改</el-button>
      </el-form-item>
    </el-form>

  </el-dialog>
  <el-form :inline="true" :model="formInline" class="demo-form-inline">

    <el-form-item label="盘点单号">
      <el-input v-model="formInline.InventoryAudit_Code" placeholder="请输入编号" clearable />
    </el-form-item>
    <el-form-item label="盘点结果">
      <el-input v-model="formInline.Inventory_Result" placeholder="请输入菜单名称" clearable />
    </el-form-item>
    <el-form-item label="盘点类型">
      <el-input v-model="formInline.Inventory_Type" placeholder="请输入菜单名称" clearable />
    </el-form-item>
    <el-form-item label="仓库名称">
      <el-input v-model="formInline.Warehouse_Name" placeholder="请输入菜单名称" clearable />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="Show()">查询</el-button>
    </el-form-item>
  </el-form>

  <el-form :inline="true">
    <el-form-item>
      <el-button type="primary" @click="AddOppen()">新增</el-button>
      <el-button type="primary" @click="OppenExports()">导出</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column prop="inventoryAudit_Id" label="序号" width="auto">
      <template v-slot="scope">
        <el-checkbox-group v-model="addIds">
          <el-checkbox :value="scope.row.inventoryAudit_Id">{{ scope.row.inventoryAudit_Id }}</el-checkbox>
        </el-checkbox-group>
      </template>
    </el-table-column>
    <el-table-column prop="inventoryAudit_Code" label="盘点单号" width="auto" >
      <template v-slot="scope">
        <span @click="Gochange(scope.row.inventoryAudit_Id)" style="color: green;">{{ scope.row.inventoryAudit_Code }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="inventory_Result" label="盘点结果" width="auto" />
    <el-table-column prop="warehouse_Name" label="仓库名称" width="auto" />
    <el-table-column prop="inventory_Type" label="盘点类型" width="auto" />
    <el-table-column prop="inventory_Starttime" label="盘点开始时间" width="auto" />
    <el-table-column prop="inventory_Endtime" label="盘点结束时间" width="auto" />
    <el-table-column prop="outbound_Status" label="出库状态" width="auto" />
    <el-table-column prop="inbound_Status" label="入库状态" width="auto" />
    <el-table-column prop="inventory_Shoppings" label="盘点货品" width="auto" />
    <el-table-column prop="inventory_Quantity" label="盘点数量" width="auto" />
    <el-table-column prop="profitQuantity" label="盘盈数量" width="auto" />
  </el-table>
      <table>
        <thead>
            <tr>
                <td>
                    <el-pagination v-model:current-page="Page.pageIndex" v-model:page-size="Page.pageSize"
                        :page-sizes="[15, 20, 30, 40]" :background="true"
                        layout="total, sizes, prev, pager, next, jumper,slot" :total="Page.total" prev-text="上一页"
                        next-text="下一页">
                        共{{ Page.total }}条，共{{ Page.pageIndex }}\{{ Page.totalPage }}页
                    </el-pagination>
                </td>
            </tr>
        </thead>

    </table>
  <div class="table-footer">
    <div class="batch-actions">
      <el-button size="small" @click="selectAll()">全选</el-button>
      <el-button size="small" @click="reverseSelect()">反选</el-button>
      <el-select size="small" placeholder="批量操作" style="width: 120px;">
        <el-option label="批量删除" value="delete" @click="DeleteAll()" />
        <el-option label="批量导出" value="export" @click="OppenExportsAll()" />
      </el-select>
    </div>
  </div>
</template>

<script lang="ts" setup>
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { onMounted, reactive, ref,watch } from 'vue'
// import { http } from '@/axios/axiosHelpers'
import { systeamread } from '@/systeamaxios/sysaxiosHelpers'
import { useRouter } from 'vue-router'

const router = useRouter()
const AdddialogVisible = ref(false)
const UpdatedialogVisible = ref(false)
const Oppen2 = ref(false)
const LogExport = ref(false)
const addIds = ref<any>([])
const ExportCouts = reactive({
  cout: ""
})
//表格加入
const Page = reactive({
  pageIndex: 1,
  pageSize: 15,
  total: 0,
  totalPage: 0
})
const bt = reactive({
  bid: 0
})
const formInline = reactive({
InventoryAudit_Code:"",
Inventory_Result:"",
Inventory_Type:"",
Warehouse_Name:"",
})
const Gochange=(id:number)=>{
  router.push({
    name: "InventoryAuditEdit",
    query:{
      id:id
    }
  })
}
const AddForm = reactive({
   "isDel": false,
        "inventoryAudit_Id": 0,
        "inventoryAudit_Code": "string",
        "inventory_Result": "",
        "warehouse_Name": "",
        "inventory_Type": "",
        "inventory_Starttime": "2025-08-05T08:22:08.658Z",
        "inventory_Endtime": "2025-08-05T08:22:08.658Z",
        "outbound_Status": true,
        "inbound_Status": true,
        "inventory_Shoppings": "",
        "inventory_Quantity": 0,
        "profitQuantity": 0
})
const UpdateForm = reactive({
     "isDel": false,
        "inventoryAudit_Id": 0,
        "inventoryAudit_Code": "string",
        "inventory_Result": "",
        "warehouse_Name": "",
        "inventory_Type": "",
        "inventory_Starttime": "2025-08-05T08:22:08.658Z",
        "inventory_Endtime": "2025-08-05T08:22:08.658Z",
        "outbound_Status": true,
        "inbound_Status": true,
        "inventory_Shoppings": "",
        "inventory_Quantity": 0,
        "profitQuantity": 0
})
const tableData = ref([{
     "isDel": false,
        "inventoryAudit_Id": 0,
        "inventoryAudit_Code": "string",
        "inventory_Result": "",
        "warehouse_Name": "",
        "inventory_Type": "",
        "inventory_Starttime": "2025-08-05T08:22:08.658Z",
        "inventory_Endtime": "2025-08-05T08:22:08.658Z",
        "outbound_Status": true,
        "inbound_Status": true,
        "inventory_Shoppings": "",
        "inventory_Quantity": 0,
        "profitQuantity": 0
}])

const AddOppen=()=>{
  AdddialogVisible.value = true
}

//保存选中的id
const handleSelectionChange = (selection: any[]) => {
  addIds.value = selection.map(item => item.rabit_Id)
}
//全部选中id
const selectAll = () => {
  addIds.value = tableData.value.map(item => item.inventoryAudit_Id)
}
//反选
const reverseSelect = () => {
  addIds.value = tableData.value.filter(item => !addIds.value.includes(item.inventoryAudit_Id)).map(item => item.inventoryAudit_Id)
}

const OppenExportsAll = () => {
  // let url = "http://112.126.93.146:8000/managementsysteamreadapis/api/ExportAll/?Ids=" + addIds.value.toString();
  let url = "http://localhost:5293/api/ExportAll/?Ids=" + addIds.value.toString();
  window.location.href = url
  addIds.value = []
}

const OppenExports = () => {
  LogExport.value = true
}
onMounted(() => {
  Show()
})
watch(Page,()=>{
  Show()
})
const AllExport = () => {
  // let url = "http://112.126.93.146:8000/managementsysteamreadapis/api/ExportAll/ProductOutAllows?NumCout=" + ExportCouts.cout;
  let url = "http://localhost:5293/api/ExportAll/ProductOutAllows?NumCout=" + ExportCouts.cout;
  window.location.href = url
  LogExport.value = false
}
const Show = () => {

  // http.get("/managementsysteamreadapis/api/ProductOutAllowAll/ShowProductOutAllow", {
  systeamread.get("/api/InventoryAuditsAll/ShowInventoryAudit", {
    params: {
      PageIndex: Page.pageIndex,
      PageSize: Page.pageSize,
    InventoryAudit_Code:formInline.InventoryAudit_Code,
    Inventory_Result:formInline.Inventory_Result,
    Inventory_Type:formInline.Inventory_Type,
    Warehouse_Name:formInline.Warehouse_Name,
    }
  })
    .then(res => {
      tableData.value = res.data.data.data
      Page.total = res.data.total
      Page.totalPage = res.data.totalPage
      addIds.value = []
    })
}

const DeleteAll = () => {
  // http.get("/managementsysteamreadapis/api", {
  systeamread.get("/api/InventoryAuditsAll/DelAllInventoryAudit",{
    params: {
      ids: addIds.value.toString()
    }
  }).then(res=>{
    ElMessage.success(res.data.message)
  })
}
const Delete = (id: number) => {
  if (!confirm("确定删除吗"))
    return;
  // http.delete("/managementsysteamreadapis/api/ProductOutAllowAll/DelProductOutAllow", {
  systeamread.delete("/api/InventoryAuditsAll/DelInventoryAudit", {
    params: {
    InventoryAudit_Id: id
    }
  })
    .then(res => {
      ElMessage.success(res.data.message)
      Show()
    })
}
const Edit = (id: number) => {
  bt.bid = id
  Oppen2.value = true
}

const EditAll = () => {
  // 这里应该实现批量编辑的逻辑
  // 可以根据选中的 addIds 来处理
  if (addIds.value.length === 0) {
    ElMessage.warning('请先选择要编辑的项目')
    return
  }
  
  // 这里可以打开编辑对话框或执行其他编辑操作
  ElMessage.info('批量编辑功能待实现')
}

</script>